const express = require('express')
const router = express.Router()
const commentsAction = require('../Controller/comments')
const formatTime = require('../tool/formatTime')
const check = require('../mid')





//渲染评论管理列表页面
router.get('/comments-admin', function (req, res) {  
    res.render('comments-admin.html')
})


// 编辑评论
router.get('/comments-edit', function (req, res) {

  res.render("comments-edit.html")
})

//单条评论
router.get('/cmtsInfo:id',function(req,res){
  let id= req.params.id
  res.render('comment-info.html',{id})
})


/**
 * 
 * api
 ** */

//引用回复
router.post('/replay:id',function(req,res){
  let id = req.params.id
  let rebody = req.body
  if(req.session.user){
    console.log(rebody)
    rebody.replyName=req.session.user.name
  }
  commentsAction.reCmts(id,rebody).then(function(doc){
    let len = doc.replyBody.length
    let newReplay = doc.replyBody[len-1]
    newReplay.time = formatTime(newReplay.time) 
    res.json(newReplay)
  })
})


//一般回复
router.post('/comments',function(req,res){
  if(req.session.user){
    req.body.replyName= req.session.user.name
    req.body.img = req.session.user.Avatar
  }else{
    req.body.img = '/public/static/images/tx'+Math.floor(Math.random()*(10-1+1)+1)+'.jpg'
  }
  let comment = req.body
  commentsAction.setCmts(comment).then(function(doc){
    doc.time = formatTime(doc.time)
    res.json({code:1,msg:doc})
  })
})

//获取文章评论
router.get('/comments:id',function(req,res){
  let  artId= req.params.id;
  commentsAction.getCmts(artId).then(function(doc){
    let comments = doc
    comments.forEach(item => {
      item.time = formatTime(item.time)
       item.replyBody.forEach(item2=>{
        item2.time = formatTime(item2.time)
       })
    });
    res.json(comments)
  })
})

//获取所有评论
router.get('/getAllCmts',function(req,res){
  commentsAction.all(function(doc){
    doc.forEach(item=>{
      item.time = formatTime(item.time)
    })
    res.json(doc)
  })
})


//审核pinglun 
router.get('/pastCmts',check.passCmts,function(req,res){
  let past = req.query.arr
  commentsAction.pastById(past,true,function(doc){
    if(doc.ok==1){
      res.json({code:1,msg:'更新成功'})
    }else{
      res.json({code:0,msg:'更新失败'})
    }  
  })
})

//关闭评论
router.get('/refuseCmt',check.passCmts,function(req,res){
  let past = req.query.arr
  commentsAction.pastById(past,false,function(doc){
    if(doc.ok==1){
      res.json({code:1,msg:'更新成功'})
    }else{
      res.json({code:0,msg:'更新失败'})
    }  
  })
  
})

//获取单条评论
router.get('/getCmtOne:id',function(req,res){
  let id= req.params.id
  commentsAction.getOne(id,function(doc){
    doc.time = formatTime(doc.time)
  
    doc.replyBody.forEach(item2=>{
      item2.time = formatTime(item2.time)
      })
    res.json(doc)
  })
})

//删除评论
router.get('/deleteCmt',check.passCmts,function(req,res){
  let dearr = req.query.arr
  commentsAction.deById(dearr,function(doc){
    if(doc.ok==1){
      res.json({code:1,msg:'更新成功'})
    }else{
      res.json({code:0,msg:'更新失败'})
    } 
  })

})



module.exports = router